package com.oocl.mylink;

import java.util.Comparator;

import com.oocl.model.Customer;
import com.oocl.rcm.Entry;

public class LinkedList{
	private Node begin = null;
	private Node end = null;
	private int size;
	
	public LinkedList(){
		//init();
		this.begin = new Node(null);;
		this.end = new Node(null);
		begin.setNext(end);
		end.setPre(begin);
		size = 0;
	}
	
	public void init(){
		
	}
	
	public void add(Node newnode){
		add(this.size,newnode);
	}
	public void add(Customer customer){
		add(this.size,customer);
	}
	public void add(int index,Customer customer){
		Node node = this.begin;
		for(int i = 0; i<index;i++){
			node = node.getNext();
		}
		Node newnode = new Node(customer);
		newnode.setNext(node.getNext());
		newnode.setPre(node);
		node.getNext().setPre(newnode);
		node.setNext(newnode);	
		size++;
	}
	
	
	public void add(int index, Node newnode){
		Node node = this.begin;
		for(int i = 0; i<index;i++){
			node = node.getNext();
		}
		newnode.setNext(node.getNext());
		newnode.setPre(node);
		node.getNext().setPre(newnode);
		node.setNext(newnode);	
		size++;
	}
	public void remove(Node node){
		node.getNext().setPre(node.getPre());
		node.getPre().setNext(node.getNext());
		size--;
	}
	public boolean remove(String id){
		Node node = this.begin;
		for (int i = size; i > 0; i--) {
			node = node.getNext();
			if((node.getData().getId()).equals(id)){
				node.getNext().setPre(node.getPre());
				node.getPre().setNext(node.getNext());
				size--;
				return true;
			}
		}
		return false;
	}
	
	public boolean update(Customer customer){
		Node node = this.begin;
		for (int i = 0; i < size; i++) {
			node = node.getNext();
			if((node.getData().getId()).equals(customer.getId())){
//				node.setData(customer);
				if(customer.getName()!=null){
					node.getData().setName(customer.getName());
				}
				if(customer.getSex()!=null){
					node.getData().setSex(customer.getSex());
				}
				if(customer.getBirthday()!=null){
					node.getData().setBirthday(customer.getBirthday());
				}
				if(customer.getAddress()!=null){
					node.getData().setAddress(customer.getAddress());
				}
				if(customer.getCall()!=null){
					node.getData().setCall(customer.getCall());
				}
				
				return true;
			}
		}
		return false;
	}
	
	public Node[] getById(Node[] nodes,String id){
		Node[] results = new Node[1];
		int size = 1;
		Node node = this.begin;
		for(int i=0;i<nodes.length;i++){
			node = node.getNext();
			if(node!=null){
				if((node.getData().getId()).equals(id)){
					Node[] temp = new Node[++size];
					for(int j=0;j<results.length;j++){
						temp[j] = results[j];
					};
					temp[size-1] = node;
					results = temp;
				}
			}
		}
		return results;
	}
	
	public Node[] getBySex(Node[] nodes,String sex){
		Node[] results = new Node[1];
		int size = 1;
		Node node = this.begin;
		for(int i=0;i<nodes.length;i++){
			node = node.getNext();
			if(node!=null){
				if((node.getData().getSex()).equals(sex)){
					Node[] temp = new Node[++size];
					for(int j=0;j<results.length;j++){
						temp[j] = results[j];
					};
					temp[size-1] = node;
					results = temp;
				}
			}
		}
		return results;
	}
	public Node[] getByName(Node[] nodes,String name){
		Node[] results = new Node[1];
		int size = 1;
		Node node = this.begin;
		for(int i=0;i<nodes.length;i++){
			node = node.getNext();
			if(node!=null){
				if((node.getData().getName()).equals(name)){
					Node[] temp = new Node[++size];
					for(int j=0;j<results.length;j++){
						temp[j] = results[j];
					};
					temp[size-1] = node;
					results = temp;
				}
			}
		}
		return results;
	}
	public Node[] getByAddress(Node[] nodes,String address){
		Node[] results = new Node[1];
		int size = 1;
		Node node = this.begin;
		for(int i=0;i<nodes.length;i++){
			node = node.getNext();
			if(node!=null){
				if((node.getData().getAddress()).equals(address)){
					Node[] temp = new Node[++size];
					for(int j=0;j<results.length;j++){
						temp[j] = results[j];
					};
					temp[size-1] = node;
					results = temp;
				}
			}
		}
		return results;
	}
	public Node[] getByBirth(Node[] nodes,String birthday){
		Node[] results = new Node[1];
		int size = 1;
		Node node = this.begin;
		for(int i=0;i<nodes.length;i++){
			node = node.getNext();
			if(node!=null){
				if((node.getData().getBirthday()).equals(birthday)){
					Node[] temp = new Node[++size];
					for(int j=0;j<results.length;j++){
						temp[j] = results[j];
					};
					temp[size-1] = node;
					results = temp;
				}
			}
		}
		return results;
	}
	public Node[] getByCall(Node[] nodes,String call){
		Node[] results = new Node[1];
		int size = 1;
		Node node = this.begin;
		for(int i=0;i<nodes.length;i++){
			node = node.getNext();
			if(node!=null){
				if((node.getData().getCall()).equals(call)){
					Node[] temp = new Node[++size];
					for(int j=0;j<results.length;j++){
						temp[j] = results[j];
					};
					temp[size-1] = node;
					results = temp;
				}
			}
		}
		return results;
	}
	
	public Node[] getNodes(){
		Node[] nodes = new Node[this.size]; 
		Node node = this.begin;
		for(int i=0;i<this.size;i++){
			nodes[i] = node;
			node = node.getNext();
		}
		
		return nodes;
	}
	
	public Customer[] toCusArray(){
		Customer[] cus = new Customer[size];
		Node node = this.begin;
		for (int i = 0; i < size; i++) {
			node = node.getNext();
			if(node.getData()!=null){
				cus[i] = node.getData();
			}
		}
		return cus;
	}
//	public String queryByName(String name){
//		
//	}
//	public String queryById(String id){
//		
//	}
//	public void sort(){
//		
//		for(int i = 0 ; i<size-1; i++){
//			Node node = begin.getNext();
//			for(int j = 0; j<size-i-1;j++){
//				if(node.getData()<node.getNext().getData()){
//					int temp = node.getData();
//					node.setData(node.getNext().getData());
//					node.getNext().setData(temp);
//				
//					node = node.getNext();
//				}
//			}
//		}
//	}  
	
	public void show(){
		Node node = begin.getNext();
		for(int i = 0 ; i<size;i++){
			System.out.println(node.getData() + "  \t");
			node = node.getNext();
		}
	}
	
	public int getSize(){
		return this.size;
	}
//	public static void main(String[] args) {
//		LinkedList list = new LinkedList();
//		Node node1 = new Node(1);
//		Node node2 = new Node(2);
//		Node node3 = new Node(3);
//		Node node4 = new Node(4);
//		Node node8 = new Node(8);
//		list.add(node1);
//		list.add(node2);
//		list.add(node3);
//		list.add(node4);
//		System.out.println("Create a new list:");
//		list.show();
//		list.add(3,node8);
//		System.out.println("Insert a node:");
//		list.show();
//		list.sort();
//		System.out.println("The order after sort:");
//		list.show();
//		list.remove(node3);
//		System.out.println("Remove a node:");
//		list.show();
//	}
}
